package com.asset.dispoal.user.domain.redis.commom;

import com.asset.dispoal.user.domain.redis.entity.UserRedis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tech.mhuang.pacebox.core.util.StringUtil;
import tech.mhuang.pacebox.springboot.redis.commands.IRedisExtCommands;

/**
 * @author qr
 * @date 2022/1/24$
 */
@Component
public class UserLoginSaveCommand {

    private static final String PREFIX_USER_ID = "sys_user_id_";


    private static final String PREFIX_USER_ACCOUNT = "sys_user_account_";


    @Autowired
    private IRedisExtCommands redisExtCommands;


    public void setUser(UserRedis userRedis) {
        redisExtCommands.hset(PREFIX_USER_ID, userRedis.getId(), userRedis);
        if (!StringUtil.isBlank(userRedis.getAccount() + "_" + userRedis.getType())) {
            redisExtCommands.hset(PREFIX_USER_ACCOUNT, userRedis.getAccount() + "_" + userRedis.getType(), userRedis);
        }
    }

    public UserRedis getUserByAccount(String username) {
        return redisExtCommands.hget(PREFIX_USER_ACCOUNT, username, UserRedis.class);
    }

}
